/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.common.exception;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import com.tompai.common.config.Constant;
import com.tompai.common.domain.LogDO;
import com.tompai.common.service.LogService;
import com.tompai.common.utils.HttpServletUtils;
import com.tompai.common.utils.MsgResult;
import com.tompai.common.utils.ShiroUtils;
import com.tompai.system.domain.UserDO;

/**
 * 异常处理器
 */
@RestControllerAdvice
public class BDExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	LogService logService;
//
//    /**
//     * 自定义异常
//     */
//    @ExceptionHandler(BDException.class)
//    public R handleBDException(BDException e) {
//        logger.error(e.getMessage(), e);
//        R r = new R();
//        r.put("code", e.getCode());
//        r.put("msg", e.getMessage());
//        return r;
//    }
//
//    @ExceptionHandler(DuplicateKeyException.class)
//    public R handleDuplicateKeyException(DuplicateKeyException e) {
//        logger.error(e.getMessage(), e);
//        return R.error("数据库中已存在该记录");
//    }
//
//    @ExceptionHandler(org.springframework.web.servlet.NoHandlerFoundException.class)
//    public R noHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException e) {
//        logger.error(e.getMessage(), e);
//        return R.error(404, "没找找到页面");
//    }

	@ExceptionHandler(AuthorizationException.class)
	public Object handleAuthorizationException(AuthorizationException e, HttpServletRequest request) {
		logger.error(e.getMessage(), e);
		if (HttpServletUtils.jsAjax(request)) {
			return MsgResult.error(403, "未授权");
		}
		return new ModelAndView("error/403");
	}

	@ExceptionHandler({ Exception.class })
	public Object handleException(Exception e, HttpServletRequest request) {
		LogDO logDO = new LogDO();
		logDO.setGmtCreate(new Date());
		logDO.setOperation(Constant.LOG_ERROR);
		logDO.setMethod(request.getRequestURL().toString());
		logDO.setParams(e.toString());
		UserDO current = ShiroUtils.getUser();
		if (null != current) {
			logDO.setUserId(current.getUserId());
			logDO.setUsername(current.getUsername());
		}
		logService.save(logDO);
		logger.error(e.getMessage(), e);
		if (HttpServletUtils.jsAjax(request)) {
			return MsgResult.error(500, "服务器错误，请联系管理员");
		}
		return new ModelAndView("error/500");
	}
}
